//
//  ZZLCTradeHistoryVC.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/15.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "ZZLCTradeHistoryVC.h"
#import "TradeHistoryTableViewCell.h"
#import "TradeHistoryMonthModel.h"
#import "TradeHistoryModel.h"
@interface ZZLCTradeHistoryVC ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
{
    NSInteger _pageIndex;
    NSString *_counterStr;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,assign) NSInteger currentPage;
@end

@implementation ZZLCTradeHistoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"交易记录";
    self.tableView.backgroundColor = COLORE_GLOBLE_BACKGOUNDCOLOR;
    [self configureTableView];
    _currentPage = 1;
    [self loadData];
    // Do any additional setup after loading the view from its nib.
}
- (void)loadData{
//    [NetWorkingManager postWithRequestUrl:@"User/findFundRecord" AndParameters:@{@"parameters":[@{@"pageSize":@"20",@"currentPage":@"1",@"authorization":[UserModel authorization]?[UserModel authorization]:@""} DataTOjsonStringAndAddVersion]} AndRequestBlock:^(NSDictionary *responseObject) {
//        self.dataSource = [TradeHistoryMonthModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]];
//        [self.tableView reloadData];
//    } FailBlock:^(NSDictionary *errDic) {
//
//    }];
    
    [NetWorkingManager postWithRequestUrl:@"User/findFundRecord" AndParameters:@{@"parameters":[@{@"pageSize":@"20",@"currentPage":@(_currentPage),@"authorization":[UserModel authorization]?[UserModel authorization]:@""} DataTOjsonString]} AndRequestBlock:^(NSDictionary *responseObject) {
        if (_currentPage == 1) {
            self.dataSource = [TradeHistoryMonthModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]];
        }else{
            [self.dataSource addObjectsFromArray: [TradeHistoryMonthModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]]];
        }
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
        
    } FailBlock:^(NSDictionary *errDic) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];
    
}
- (void)configureTableView
{
    _tableView.emptyDataSetSource = self;
    _tableView.emptyDataSetDelegate = self;
    _tableView.backgroundColor = RGBCOLOR(245, 245, 245);
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage = 1;
        [self loadData];
    }];
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _currentPage += 1;
        [self loadData];
    }];
}
#pragma mark
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    TradeHistoryMonthModel *model = self.dataSource[section];
    NSArray *rowDatasource = [TradeHistoryModel mj_objectArrayWithKeyValuesArray:model.list];
    return rowDatasource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"TradeHistoryTableViewCell" owner:self options:nil];
    TradeHistoryTableViewCell *cell = [nibs firstObject];
    TradeHistoryMonthModel *modelMonth = self.dataSource[indexPath.section];
    NSArray *rowDatasource = [TradeHistoryModel mj_objectArrayWithKeyValuesArray:modelMonth.list];
    TradeHistoryModel *model = rowDatasource[indexPath.row];
    [cell configureCellByModel:model];

    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    view.backgroundColor = COLORE_GLOBLE_BACKGOUNDCOLOR;
    UILabel *dateLabel = [[UILabel alloc]init];
    [view addSubview:dateLabel];
    dateLabel.font = Font(13);
    dateLabel.textColor = RGBCOLOR(128, 128, 128);
    TradeHistoryMonthModel *model = self.dataSource[section];
    dateLabel.text = model.date;
    [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.centerY.equalTo(@0);
    }];
    return view;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 106;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
